﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace Web_HengliSeed.BLL
{
    public class ProductInfo
    {
        /// <summary>
        /// 获取一条分类数据
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetClassMes(string strClassID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_ProClassID", strClassID) };
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_ProClass WHERE T_ProClassID=@T_ProClassID ", parm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取所有的分类信息
        /// </summary>
        /// <returns></returns>
        public static OleDbDataReader GetClassMesAll(string lan)
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_ProClass  where T_Language='" + lan + "'", null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 编辑类别数据
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="strClassName"></param>
        /// <param name="strKey"></param>
        /// <param name="strDes"></param>
        /// <returns></returns>
        public static bool EditClassMes(string ClassID, string strClassName, string strKey, string strDes, string lan)
        {
            try
            {
                OleDbParameter[] parm;
                string strSql = " INSERT INTO  T_ProClass (T_ProClassName,T_ProClassKey,T_ProClassDes,T_Language) VALUES (@T_ProClassName,@T_ProClassKey,@T_ProClassDes,@T_Language) ";
                parm = new OleDbParameter[] { new OleDbParameter("@T_ProClassName", strClassName),
                                                           new OleDbParameter("@T_ProClassKey", strKey),
                                                           new OleDbParameter("@T_ProClassDes", strDes),
                                                           new OleDbParameter("@T_Language",lan),
                                                           new OleDbParameter("@T_ProClassID",ClassID)
                                                            };

                if (!string.IsNullOrEmpty(ClassID))
                {
                    strSql = "  UPDATE T_ProClass SET T_ProClassName=@T_ProClassName,T_ProClassKey=@T_ProClassKey,T_ProClassDes=@T_ProClassDes  WHERE T_ProClassID=@T_ProClassID ";
                    parm = new OleDbParameter[] { new OleDbParameter("@T_ProClassName", strClassName),
                                                           new OleDbParameter("@T_ProClassKey", strKey),
                                                           new OleDbParameter("@T_ProClassDes", strDes),
                                                           new OleDbParameter("@T_ProClassID",ClassID)
                                                            };
                }
                
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, parm) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除类别信息
        /// </summary>
        /// <param name="strClassID"></param>
        /// <returns></returns>
        public static bool DelClassMes(string strClassID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_ProClassID", strClassID) };
                bool ReClass = (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, " DELETE FROM T_ProClass WHERE T_ProClassID=@T_ProClassID ", parm) > 0) ? true : false;
                DelProductClassInfo(strClassID);
                return ReClass;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 分页查询产品信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static DataTable GetAllProductInfo(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere, string lan)
        {
            try
            {
                string strSql = " SELECT P.T_ProductID,P.T_ProClassID,P.T_ProductName,P.T_ProFlag,P.T_DateTime,P.T_ProductImage,C.T_ProClassName,C.T_ProClassKey,C.T_ProClassDes,P.T_ProductNote FROM T_ProductInfo P  " +
                                " LEFT JOIN T_ProClass C ON P.T_ProClassID=C.T_ProClassID  where P.T_Language='" + lan + "'";
                string strShow = " T_ProductID,T_ProductName,T_DateTime,T_ProductImage,T_ProFlag,T_ProClassName,T_ProClassKey,T_ProClassDes,T_ProductNote ";
                return ClassFile.AccessHelper.ExecutePager(PageIndex, PageSize, "T_ProductID", strShow, strSql, strWhere, " T_ProductID DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 编辑产品数据
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="strClassName"></param>
        /// <param name="strKey"></param>
        /// <param name="strDes"></param>
        /// <returns></returns>
        public static bool EditProductMes(string strT_ProductID, string strT_ProductName, string strT_ProClassID, string strT_ProductImage, int T_ProductFlag, string strT_ProductNote,string lan,string vPath)
        {
            try
            {
                OleDbParameter[] parm;
                string strSql = " INSERT INTO  T_ProductInfo (T_ProductName,T_ProClassID,T_ProductImage,T_ProFlag,T_ProductNote,T_DateTime,T_Language,T_VideoPath) VALUES (@T_ProductName,@T_ProClassID,@T_ProductImage,@T_ProFlag,@T_ProductNote,@T_DateTime,@T_Language,@T_VideoPath) ";
                parm = new OleDbParameter[] { new OleDbParameter("@T_ProClassName", strT_ProductName),
                                                           new OleDbParameter("@T_ProClassID", strT_ProClassID),
                                                           new OleDbParameter("@T_ProductImage", strT_ProductImage),
                                                           new OleDbParameter("@T_ProFlag", T_ProductFlag),
                                                           new OleDbParameter("@T_ProductNote", strT_ProductNote),
                                                           new OleDbParameter("@T_DateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                                                           new OleDbParameter("@T_Language", lan),
                                                           new OleDbParameter("@T_VideoPath", vPath),
                                                           new OleDbParameter("@T_ProductID", string.IsNullOrEmpty(strT_ProductID) ? 0 : Convert.ToInt32(strT_ProductID))};

                if (!string.IsNullOrEmpty(strT_ProductID))
                {
                    strSql = "UPDATE  T_ProductInfo SET T_ProductName=@T_ProductName,T_ProClassID=@T_ProClassID,T_ProductImage=@T_ProductImage,T_ProFlag=@T_ProFlag,T_ProductNote=@T_ProductNote,T_DateTime=@T_DateTime,T_VideoPath=@T_VideoPath WHERE  T_ProductID=@T_ProductID ";
                    parm = new OleDbParameter[] { new OleDbParameter("@T_ProClassName", strT_ProductName),
                                                           new OleDbParameter("@T_ProClassID", strT_ProClassID),
                                                           new OleDbParameter("@T_ProductImage", strT_ProductImage),
                                                           new OleDbParameter("@T_ProFlag", T_ProductFlag),
                                                           new OleDbParameter("@T_ProductNote", strT_ProductNote),
                                                           new OleDbParameter("@T_DateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                                                           new OleDbParameter("@T_VideoPath", vPath),
                                                           new OleDbParameter("@T_ProductID", string.IsNullOrEmpty(strT_ProductID) ? 0 : Convert.ToInt32(strT_ProductID))
                                                            };
                }
                 
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, parm) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///获取一条产品信息
        /// </summary>
        /// <param name="strProID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetProInfo(string strProID)
        {
            try
            {
                OleDbParameter[] parm = { new OleDbParameter("@T_ProductID", strProID) };
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT * FROM T_ProductInfo WHERE T_ProductID=@T_ProductID ", parm);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        ///获取所有产品名称
        /// </summary>
        /// <param name="strProID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetAllProName()
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT T_ProductID,T_ProductName FROM T_ProductInfo  ", null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        ///获取所有产品名称
        /// </summary>
        /// <param name="strProID"></param>
        /// <returns></returns>
        public static OleDbDataReader GetAllTopProMes()
        {
            try
            {
                return ClassFile.AccessHelper.ExecuteReader(ClassFile.AccessHelper.ConnectionString, " SELECT T_ProductID,T_ProductName,T_ProductImage FROM T_ProductInfo  WHERE T_ProFlag=1 ", null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除指定ID的产品
        /// </summary>
        /// <param name="strProClass"></param>
        /// <returns></returns>
        public static bool DelProductInfo(string strProID)
        {
            try
            {
                string strSql = string.Format(" DELETE FROM T_ProductInfo WHERE T_ProductID in({0}) ", strProID);
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除指定类别下的所有的产品
        /// </summary>
        /// <param name="strProClass"></param>
        /// <returns></returns>
        public static bool DelProductClassInfo(string strProClassID)
        {
            try
            {
                string strSql = string.Format(" DELETE FROM T_ProductInfo WHERE T_ProClassID ={0} ", strProClassID);
                return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0) ? true : false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}